﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using Client.Models;
using Client;

namespace CShare
{
    public class HttpPost
    {
        public RetModel PostData(string address, string path, Dictionary<string, string> param, string data)
        {
            try
            {
                //URL参数
                string pUrl = "";
                if (param != null)
                {
                    if (param.Count > 0)
                        foreach (var key in param.Keys)
                        {
                            if ("" == pUrl)
                                pUrl += key + "=" + param[key];
                            else
                                pUrl += "&" + key + "=" + param[key];
                        }
                    if ("" != pUrl)
                    {
                        if (address.IndexOf("?") < 0)
                        {
                            pUrl = "?" + pUrl;
                        }
                        else
                        {
                            pUrl = "&" + pUrl;
                        }
                    }
                }

                //返回的对象
                RetModel ret = new RetModel();
                //Http请求对象
                HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(string.Format("{0}{1}{2}", address, path, pUrl)));
                req.Method = "POST";
                //设置返回类型为json
                req.Accept = "application/json";
                req.ContentType = "application/json; charset = utf-8";
                if ("" == data)
                {
                    req.ContentLength = 0;
                }
                else
                {
                    //用streamwriter把json串写到流中      
                    System.IO.StreamWriter sw = new System.IO.StreamWriter(req.GetRequestStream());
                    sw.Write(data);
                    sw.Flush();
                    sw.Close();
                    sw = null;
                }
                Share.TotalUpLink += req.ContentLength;
                Share.TotalUpLink += req.Headers.ToByteArray().Length;

                //请求并返回response对象
                HttpWebResponse rp = null;
                try
                {
                    rp = (HttpWebResponse)req.GetResponse();
                }
                catch (WebException ex)
                {
                    rp = (HttpWebResponse)ex.Response;
                }
                //streamreader读取返回流中的所有数据
                System.IO.StreamReader sr = new System.IO.StreamReader(rp.GetResponseStream());
                string retstr = sr.ReadToEnd();
                sr.Close();
                sr = null;
                HttpStatusCode hs = rp.StatusCode;
                Share.TotalDownLink += rp.ContentLength;
                Share.TotalDownLink += rp.Headers.ToByteArray().Length;
                //判断
                if ("OK" == hs.ToString())
                {
                    try
                    {
                        ret = JsonConvert.DeserializeObject<RetModel>(retstr);
                    }
                    catch (Exception ex)
                    {
                        ret.ErrCode = "99999";
                        ret.ErrInfo = ex.Message;
                        ret.Success = false;
                    }
                    return ret;
                }
                else
                {
                    ret.Success = false;
                    ret.ErrCode = "99999";
                    ret.ErrInfo = hs.ToString();
                    return ret;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}

